<template>
    <el-drawer v-model="visibleDrawer" :title="formModel.title ? '编辑文章' : '添加文章'" direction="rtl" size="50%">
        <!-- 发表文章表单 -->
       <el-form :model="formModel" ref="formRef" label-width="100px" :rules="formRules">
        <el-form-item label="文章标题" prop="title">
            <el-input v-model="formModel.title" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="文章分类" prop="cate">
            <el-select v-model="formModel.cate" width="100%">
                <el-option label="公告" value="公告"></el-option>
                <el-option label="动态" value="动态"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="文章内容" prop="content">
            <div class="editor">
                <quill-editor theme="snow" v-model:content="formModel.content" contentType="html" ref="editorRef"></quill-editor>
            </div>
        </el-form-item>
        <el-form-item>
            <el-button @click="onPublish(formModel)" type="primary">发布</el-button>
        </el-form-item>
    </el-form>

    </el-drawer>
</template>
<script setup>
import { ref } from 'vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
// @ts-ignore
import { artPublishService, artEditService , artGetDetailService } from '@/api/article'
// import { baseURL } from '@/utils/request'
import { ElMessage } from 'element-plus'
const visibleDrawer = ref(false)
const formModel = ref({
    title: '',
    cate: '',
    content: '',
})
const formRules = {
    title: [
        { required: true, message: '请输入文章标题', trigger: 'change' },
    ],
    cate: [
        { required: true, message: '请选择文章分类', trigger: 'change' },
    ],
    content: [
        { required: true, message: '请输入文章内容', trigger: 'change' },
    ],
};
const editorRef = ref()
const EditTime=ref()
const formRef=ref()
const open = async (row) => {
    visibleDrawer.value = true
    formRef.value.resetFields()
    if (row.id) {
        const res = await artGetDetailService(row.id)
        formModel.value = res.data.data
    } else {
        formModel.value = {
            title: '',
            cate: '',
            content: '',
        }
        editorRef.value.setHTML('')
    }
}
const emit = defineEmits(['success'])
const onPublish = async (formModelObj) => {
    await formRef.value.validate()
    if (formModelObj.id) {
        const res=await artEditService(formModelObj)
        EditTime.value=res.data.data
        ElMessage.success('编辑成功')
        visibleDrawer.value = false
        emit('success', 'edit')
    } else {
        await artPublishService(formModelObj)
        ElMessage.success('添加成功')
        visibleDrawer.value = false
        emit('success', 'add')
    }
    formRef.value.resetFields()
}



defineExpose({
    open,
    EditTime
})
// @ts-ignore
</script>
<style scoped lang="scss">
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}

.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>
